<script setup lang="ts">

import router from "@/router";
import {userStorei} from "@/stores/user_store";
import {reactive, ref} from "vue";
import F_text from "@/components/common/f_text.vue";
import {userCardInfoApi, type userCardInfoType} from "@/api/user_api";
import {Message} from "@arco-design/web-vue";
import F_avatar from "@/components/common/f_avatar.vue";

const store = userStorei()

function check(val: string) {
  if (val === "logout") {
    // 注销流程
    store.userLogout()
    return
  }
  router.push({name: val})
}


const visible = ref(false)
const data = reactive<userCardInfoType>({
  "nickname": "",
  "scope": 0,
  "chatCount": 0,
  "createRoleCount": 0
})


async function show() {
  const res = await userCardInfoApi()
  if (res.code) {
    Message.error(res.msg)
    return
  }
  Object.assign(data, res.data)
  visible.value = true
}

</script>

<template>
  <div class="f_user_trigger_com">
    <a-trigger position="top" animation-name="fade-trigger" @hide="visible=false" @show="show" auto-fit-position
               :unmount-on-close="false">
      <f_avatar :title="store.userInfo.nickName" :class="{invisible: visible}" class="user_avatar_slot" :size="35"
                :image-url="store.userInfo.avatar"></f_avatar>
      <template #content>
        <div class="f_user_trigger_container">
          <f_avatar :title="store.userInfo.nickName" class="user_avatar_transition" :image-url="store.userInfo.avatar"></f_avatar>
          <f_text class="nickname">
            {{ store.userInfo.nickName }}
          </f_text>
          <div class="scope">
            <span>积分：{{ data.scope }}</span>
            <router-link :to="{name: 'scopeRecharge'}">
              <a-button type="primary" size="mini">充值</a-button>
            </router-link>
          </div>
          <div class="data">
            <div class="item">
              <div class="num">{{ data.chatCount }}</div>
              <div class="label">对话</div>
            </div>
            <div class="item">
              <div class="num">{{ data.createRoleCount }}</div>
              <div class="label">角色</div>
            </div>
          </div>

          <div class="menus">
            <div class="item" @click="check('home')">管理系统</div>
            <div class="item" @click="check('userInfo')">个人中心</div>
            <div class="item logout" @click="check('logout')">注销退出</div>
          </div>
        </div>
      </template>
    </a-trigger>
  </div>
</template>

<style lang="less">
.f_user_trigger_container {
  width: 200px;
  border-radius: 5px;
  background-color: var(--color-bg-1);
  box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 0.15);
  position: relative;
  padding: 50px 0 0 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  color: var(--color-text-2);

  .user_avatar_transition {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    top: -30px;
    width: 75px;
    height: 75px;
    transition: all .3s ease-out;
  }

  .nickname {
    font-size: 20px;
  }

  .scope {
    margin: 10px 0;

    a {
      margin-left: 5px;
    }
  }

  .data {
    display: flex;
    width: 100%;
    margin: 10px 0;

    .item {
      width: 50%;
      display: flex;
      flex-direction: column;
      align-items: center;

      .num {
        font-size: 18px;
        font-weight: 600;
      }
    }
  }

  .menus {
    width: 100%;

    .item {
      height: 40px;
      display: flex;
      align-items: center;
      border-bottom: @f_border;
      justify-content: center;
      cursor: pointer;
      width: 100%;

      &:last-child {
        border-bottom: none;
        margin-bottom: 5px;
      }

      &.logout {
        color: red;
      }
    }
  }
}

.f_user_trigger_com {
  .user_avatar_slot {
    opacity: 1;
    transition: all .3s;

    &.invisible {
      opacity: 0;
    }
  }
}


// 组件刚开始离开
.fade-trigger-leave-active {
}

// 组件离开结束
.fade-trigger-leave-to {
  transform: translateY(-10px);
  opacity: 0;
}

// 组件刚开始进入
.fade-trigger-enter-active {
  transform: translateY(-10px);
  opacity: 0;

  .user_avatar_transition {
    top: -35px;
    width: 35px !important;
    height: 35px !important;
  }
}

// 组件进入完成
.fade-trigger-enter-to {
  transform: translateY(0);
  opacity: 1;
}

// 正在进入和离开
.fade-trigger-leave-active, .fade-trigger-enter-active {
  transition: all .3s ease-out;
}

</style>